﻿using GKQC.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace GKQC.DbHelper.ViewModels
{
    /// <summary>
    /// 檢測標準視圖
    /// </summary>
    public class StandardView
    {
        /// <summary>
        /// 本体直径
        /// </summary>		
        public double DMax { get; set; }
		public double DMin { get; set; }
		/// <summary>
		/// 本体高度
		/// </summary>		
		public double LMax { get; set; }
		public double LMin { get; set; }
		/// <summary>
		/// 脚距
		/// </summary>		
		public double PMax { get; set; }
		public double PMin { get; set; }
		/// <summary>
		/// 引脚直径d Max
		/// </summary>		
		public double cpMax { get; set; }
		/// <summary>
		/// 引脚直径d Min
		/// </summary>
		public double cpMin { get; set; }
		/// <summary>
		/// 容量下限
		/// </summary>		
		public double CapMin { get; set; }
		/// <summary>
		/// 容量上限
		/// </summary>		
		public double CapMax { get; set; }

		public double Z2Max { get; set; }
		/// <summary>
		/// ESR上限(mΩ)
		/// </summary>		
		public double EsrMax { get; set; }
		/// <summary>
		/// Z值上限(mΩ)
		/// </summary>		
		public double ZMax { get; set; }

		/// <summary>
		/// LC最大值(μA)
		/// </summary>		
		public double LcMax { get; set; }

		/// <summary>
		/// DF最大值(%)
		/// </summary>		
		public double DfMax { get; set; }

		public double HMax { get; set; }
		public double HMin { get; set; }
		public double WMax { get; set; }
		public double WMin { get; set; }
		public double CMax { get; set; }
		public double CMin { get; set; }
		public double L1Max { get; set; }
		public double L1Min { get; set; }
		public double L2Max { get; set; }
		public double L2Min { get; set; }
		public double L3Max { get; set; }
		public double L3Min { get; set; }
        public static string isValidString { get; set; } = "isValid";

        /// <summary>
        /// 判断测试值是否合格
        /// </summary>
        /// <param name="itemCode"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool IsValid(ItemCode itemCode, double value)
        {
            bool flag = true;

            try
            {
                switch (itemCode)
                {
                    case ItemCode.CAP:
                        flag = !(value > CapMax || value < CapMin);
                        break;
                    case ItemCode.DF:
                        flag = value <= DfMax;
                        break;
                    case ItemCode.LC:
                    case ItemCode.LC2:
                        flag = value <= LcMax;
                        break;
                    case ItemCode.Z:
						flag = value <= ZMax;
                        break;
                    case ItemCode.Z2:
						flag = value <= Z2Max;
                        break;
                    case ItemCode.ESR:
					case ItemCode.ESR2:
						flag = value <= EsrMax;
                        break;
					case ItemCode.D:
						flag = !(value > DMax || value < DMin);
						break;
					case ItemCode.L:
						flag = !(value > LMax || value < LMin);
						break;
                    case ItemCode.P:
                        flag = !(value > PMax || value < PMin);
                        break;
                    case ItemCode.cp:
						flag = !(value > cpMax || value < cpMin);
						break;
					case ItemCode.H:
						flag = !(value > HMax || value < HMin);
						break;
					case ItemCode.W:
						flag = !(value > WMax || value < WMin);
						break;
					case ItemCode.C:
						flag = !(value > CMax || value < CMin);
						break;
					case ItemCode.L1:
						flag = !((value > L1Max || value < L1Min) && L1Max>0);
						break;
					case ItemCode.L2:
						flag = !((value > L2Max || value < L2Min) && L2Max > 0);
						break;
					case ItemCode.L3:
						flag = !((value > L3Max || value < L3Min) && L3Max > 0);
						break;
					default:
                        break;
                }
            }
            catch (Exception ex)
            {
				Logger.LogError(ex);
				throw ex;
            }

            return flag;
        }

        readonly string A2 = "A2";//老化硬度 比较差值范围，不是百分比
        /// <summary>
        /// 判断测试值是否合格
        /// </summary>
        /// <param name="itemCode"></param>
        /// <param name="value"></param>
        /// <param name="compareValue">比较值</param>
        /// <returns></returns>
        public bool IsValid(string item, double value, double compareValue=0)
        {
            bool flag = true;

            var proMax = this.GetType().GetProperty(item + MaxString);
            if (proMax == null)
                proMax = this.GetType().GetProperty(item + MaxString, BindingFlag);
            if (proMax != null)
            {
                double maxDouble = 0;
                var max = proMax.GetValue(this, null);

                if (max != null && double.TryParse(max.ToString(), out maxDouble))
                {
                    if (item == A2) ////老化硬度 比较差值范围，不是百分比
                    {
                        if ((compareValue - value) > maxDouble) return false;
                        else return true;
                    }
                    else
                    {
                        if (compareValue > 0)
                        {
                            //if (compareValue < value)
                            {
                                double rate = Math.Abs((value - compareValue) / compareValue * 100);
                                if (maxDouble > 0 && rate > maxDouble)
                                    flag = false;
                            }
                        }
                        else
                        {
                            if (maxDouble > 0 && value > maxDouble)
                                flag = false;
                        }
                    }
                }
            }
            var proMin = this.GetType().GetProperty(item + MinString);
            if (proMin == null)
                proMin = this.GetType().GetProperty(item + MinString, BindingFlag);
            if (proMin != null)
            {
                double minDouble = 0;
                var min = proMin.GetValue(this, null);
                if (min != null && double.TryParse(min.ToString(), out minDouble))
                {
                    if (compareValue > 0)
                    {
                        //if (compareValue > value)
                        {
                            double rate = Math.Abs((compareValue - value) / compareValue * 100);
                            if (rate < minDouble)
                                flag = false;
                        }
                    }
                    else
                    {
                        if (value < minDouble)
                            flag = false;
                    }
                }
            }

            return flag;
        }

        public readonly string MinString = "Min";
        public readonly string MaxString = "Max";
        public readonly BindingFlags BindingFlag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;

        #region 固态电容尺寸标准值
        /// <summary>
        /// 固态电容底座宽度标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetW(double D, double L, double range)
        {
            double W = 0;
            range = 0.2;

            if (D == 5 && (L == 6 || L == 7)) W = 5.3;
            else if (D == 6.3 && (L == 6 || L == 7 || L == 8 || L == 9.5)) W = 6.6;
            else if (D == 8 && (L == 7 || L == 8 || L == 9 || L == 12)) W = 8.3;
            else if (D == 10 && (L == 10 || L == 12)) W = 10.3;

            return W;
        }

        /// <summary>
        /// 固态电容底座高度标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetH(double D, double L, double range)
        {
            double H = 0;
            range = 0.2;

            if (D == 5 && (L == 6 || L == 7)) H = 5.3;
            else if (D == 6.3 && (L == 6 || L == 7 || L == 8 || L == 9.5)) H = 6.6;
            else if (D == 8 && (L == 7 || L == 8 || L == 9 || L == 12)) H = 8.3;
            else if (D == 10 && (L == 10 || L == 12)) H = 10.3;

            return H;
        }

        /// <summary>
        /// 固态电容本体直径标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetD(double D, double L, out double range)
        {
            double DD = D;
            range = 0.5;

            if (D >= 10) DD = DD + 0.5;

            return DD;
        }

        /// <summary>
        /// 固态电容本体高度标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetL(double D, double L, out double range)
        {
            double LL = L;
            if (D < 8) range = 0.3;
            else range = 0.5;

            if (D == 8 && (L == 8 || L == 9 || L == 12)) LL = 9.5;
            else if (D == 10 && (L == 12)) LL = 12.6;

            return LL;
        }

        /// <summary>
        /// 固态电容本体脚长标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetC(double D, double L, out double range)
        {
            double C = 0;
            range = 0.2;

            if (D == 5 && (L == 6 || L == 7)) C = 5.9;
            else if (D == 6.3 && (L == 6 || L == 7 || L == 8 || L == 9.5)) C = 7.2;
            else if (D == 8 && (L == 7 || L == 8 || L == 9 || L == 12)) C = 9.0;
            else if (D == 10 && (L == 10 || L == 12)) C = 11;

            return C;
        }

        /// <summary>
        /// 固态电容本体脚距标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double GetP(double D, double L, out double range)
        {
            double P = 0;
            range = 0.2;

            if (D == 5 && (L == 6 || L == 7)) P = 1.4;
            else if (D == 6.3 && (L == 6 || L == 7 || L == 8 || L == 9.5)) P = 2.2;
            else if (D == 8 && (L == 7 || L == 8 || L == 9 || L == 12)) P = 3.2;
            else if (D == 10 && (L == 10 || L == 12)) P = 4.5;

            return P;
        }


        /// <summary>
        /// 固态电容本体cp线直径标准
        /// </summary>
        /// <param name="D"></param>
        /// <param name="L"></param>
        /// <param name="range">±上下限</param>
        /// <returns></returns>
        public static double Getcp(double D, double L, out double range)
        {
            double cp = 0;
            range = 0.05;

            if (D == 5 && (L == 6 || L == 7)) cp = 0.65;
            else if (D == 6.3 && (L == 6 || L == 7 || L == 8 || L == 9.5)) cp = 0.65;
            else if (D == 8 && L == 7) cp = 0.65;
            else if (D == 8 && (L == 8 || L == 9 || L == 12))
            {
                cp = 0.9;
                range = 0.1;
            }
            else if (D == 10 && (L == 10 || L == 12))
            {
                cp = 0.9;
                range = 0.1;
            }

            return cp;
        }

        /// <summary>
        /// 获取温度试验后的LC标准倍数
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static double GetTempLcTimes(double temp)
        {
            double times = 1;
            if (temp >= 125) times = 10;
            else if (temp >= 100 && temp < 125)
                times = 8;
            else if (temp <= 85 && temp >=50)
                times = 5;

            return times;
        }

        #endregion
    }
}
